package remote.controls
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import remote.events.SliderEvent;
	import remote.geom.Size;
	import remote.skins.SolidRectangleSkin;
	
	
	[Event(type="remote.events.SliderEvent", name="change")]
	/**
	 * 滑块基类，请勿直接使用此类，而是使用HSlider和VSlider
	 * @author 银魂飞雪
	 * @createDate 2011-3-10
	 */
	public class SliderBase extends Component implements ICounting
	{
		protected var _minValue:Number = 0;
		protected var _maxValue:Number = 100;
		protected var _value:Number = 0;
		protected var _liveDragging:Boolean = false;
		protected var _stepSize:Number = 0;
		
		protected var _track:Button;
		protected var _thumb:Button;
		
		public function SliderBase()
		{
			super();
		}
	
		override protected function createChildren():void
		{
			super.createChildren();
			_track = new Button();
			_track.skin = new SolidRectangleSkin();
			_track.buttonMode = false;
			_thumb = new Button();
			addChild(_track);
			addChild(_thumb);
			_thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_mouseDownHandler);
		}
		
		override public function updateSize():void
		{
			var oldSize:Size = new Size(width, height);
			super.updateSize();
			if(!oldSize.equal(new Size(width, height)))
				this.updateChildren();
		}
		
		override protected function updateChildren():void
		{
			_track.setSize(width, height);
		}
		
		private function thumb_mouseDownHandler(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
		}
		
		private function stage_mouseMoveHandler(e:MouseEvent):void
		{
			var currentValue:Number = pointToValue(mouseX, mouseY);
			if(currentValue < minValue)
				currentValue = minValue;
			if(currentValue > maxValue)
				currentValue = maxValue;
			var pos:Point = valueToPoint(currentValue);
			if(!pos.equals(new Point(_thumb.x, _thumb.y)))
				_thumb.move(pos.x, pos.y);
			if(liveDragging && currentValue != _value)
			{
				var oldValue:Number = _value;
				_value = currentValue;
				this.dispatchEvent(new SliderEvent(SliderEvent.CHANGE, _value, oldValue));
			}
		}
		
		private function stage_mouseUpHandler(e:MouseEvent):void
		{	
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
			var currentValue:Number = pointToValue(mouseX, mouseY);
			if(currentValue < minValue)
				currentValue = minValue;
			if(currentValue > maxValue)
				currentValue = maxValue;
			if(currentValue != _value)
			{
				var pos:Point = valueToPoint(currentValue);
				_thumb.move(pos.x, pos.y);
				var oldValue:Number = _value;
				_value = currentValue;
				this.dispatchEvent(new SliderEvent(SliderEvent.CHANGE, _value, oldValue));
			}
		}
		
		/**
		 * 计算当前值 
		 * 
		 */		
		protected function pointToValue(x:Number, y:Number):Number
		{
			return 0;
		}
		
		protected function valueToPoint(value:Number):Point
		{
			return new Point();
		}
		protected function dragRect():Rectangle
		{
			return new Rectangle();
		}
		public function get minValue():Number
		{
			return _minValue;
		}
		
		public function set minValue(value:Number):void
		{
			if(_minValue != value)
			{
				_minValue = value;
				var pos:Point = this.valueToPoint(_value);
				this._thumb.move(pos.x, pos.y);
			}
		}
		
		public function get maxValue():Number
		{
			return _maxValue;
		}
		
		public function set maxValue(value:Number):void
		{
			if(_maxValue != value)
			{
				_maxValue = value;
				var pos:Point = this.valueToPoint(_value);
				this._thumb.move(pos.x, pos.y);
			}
		}
		
		public function get value():Number
		{
			return _value;
		}
		
		public function set value(value:Number):void
		{
			if(value != _value)
			{
				_value = value;
				var pos:Point = this.valueToPoint(value);
				this._thumb.move(pos.x, pos.y);
			}
		}

		public function get liveDragging():Boolean
		{
			return _liveDragging;
		}

		public function set liveDragging(value:Boolean):void
		{
			_liveDragging = value;
		}

		public function get stepSize():Number
		{
			return _stepSize;
		}

		public function set stepSize(value:Number):void
		{
			_stepSize = value;
		}

		public function get thumb():Button
		{
			return _thumb;
		}

		public function get track():Button
		{
			return _track;
		}


	}
}